﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class KhachHangDAO
    {
        public DataTable LayDanhSachKhachHang()
        {
            DataTable dt = new DataTable();
            SqlConnection cn = DataProvider.ConnectionData();
            string sql = "sp_LayDanhSachKhachHang";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            cn.Close();
            return dt;
        }
        public void ThemKhachHang(KhachHangDTO khachhang)
        {
            SqlConnection cn = DataProvider.ConnectionData();
            string sql = "sp_ThemKhachHang";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@tenKH", khachhang.TenKH);
            cmd.Parameters.Add("@sDT", khachhang.SDT);
            cmd.Parameters.Add("@diaChi", khachhang.DiaChi);
            cmd.Parameters.Add("@email", khachhang.Email);
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public void CapNhatKhachHang(KhachHangDTO khachhang)
        {
            SqlConnection cn = DataProvider.ConnectionData();
            string sql = "sp_CapNhatKhachHang";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maKH", khachhang.MaKH);
            cmd.Parameters.Add("@tenKH", khachhang.TenKH);
            cmd.Parameters.Add("@sDT", khachhang.SDT);
            cmd.Parameters.Add("@diaChi", khachhang.DiaChi);
            cmd.Parameters.Add("@email", khachhang.Email);
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public void XoaKhachHang(KhachHangDTO khachhang)
        {
            SqlConnection cn = DataProvider.ConnectionData();
            string sql = "sp_XoaKhachHang";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maKH", khachhang.MaKH);
            cmd.ExecuteNonQuery();
            cn.Close();
        }
        public DataTable TimKH(string HoTen, string SoDT)
        {
            DataTable dt = new DataTable();
            SqlConnection cn = DataProvider.ConnectionData();
            SqlCommand cmd = new SqlCommand("sp_TimKiemKH", cn);
            cmd.CommandType = CommandType.StoredProcedure;

            cmd.Parameters.Add("@HoTen", HoTen);
            cmd.Parameters.Add("@SoDT", SoDT);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            cn.Close();
            return dt;
        }
        public DataTable TimKhachHang(int maKH)
        {
            DataTable dt = new DataTable();
            SqlConnection cn = DataProvider.ConnectionData();
            string sql = "sp_TimKiemKhachHang";
            SqlCommand cmd = new SqlCommand(sql, cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maKH", maKH);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            cn.Close();
            return dt;
        }
    }
}
